package com.common.controller;

import com.common.model.Result;
import com.common.service.LoginService;
import com.common.utils.KaptchHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Autowired
    private LoginService loginService;


    @RequestMapping(value = "/platform/login", method = RequestMethod.POST)
    @ResponseBody
    public Result login(HttpServletRequest request, HttpServletResponse response) throws Exception {

        Result result = new Result();

        HttpSession session = request.getSession();

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        try {
           KaptchHelper.checkVerifyCode(request);
           loginService.login(username, password);
        } catch (Exception e) {
           e.printStackTrace();
           result.setCode(-1);
           result.setMsg(e.getMessage());
        }

        return result;

    }
}
